#include "GameLogical.h"



#define MAX_ENEMY_PETS  3


GameNormalLogical::GameNormalLogical(QObject *parent) :
    QObject(parent)
{
    m_pCommonGameData = nullptr;
}

QList<_Pet*> &GameNormalLogical::listPets()
{
    return m_pCommonGameData->m_listPets;
}



GameFightLogical::GameFightLogical(QObject *parent) :
    GameModel(parent)
{
    m_eWinner = _Pet::JiaoSe_NULL;
    m_pCommonGameData = nullptr;

    int i;
    for(i = 0; i < MAX_ENEMY_PETS; i++)
    {
        _Pet *pet = new _Pet(this);
        pet->m_eJiaoSe = _Pet::JiaoSe_Enemy;
        m_listEnemyPets.append(pet);
    }
}


int GameFightLogical::createEnemyPets()
{
    int i;
    int enemyCount = 1; //RANDOM(1, m_listEnemyPets.length());

    for(i = 0; i < enemyCount; i++)
    {
        _Pet *enemy = m_listEnemyPets.at(i);

        enemy->m_PetInfo.m_status = 0;           //状态
        enemy->m_PetInfo.m_exp = 0;                //战斗经验
        enemy->m_PetInfo.m_maxHP = 50;                //血上限        //9999
        enemy->m_PetInfo.m_remainHP = 50;            //剩余血        //9999    0表示死亡或不存在(用这个判断)
        enemy->m_PetInfo.m_attack = 5;            //攻击        //999
        enemy->m_PetInfo.m_defense = 5;            //防御
        enemy->m_PetInfo.m_speed = 5;            //速度
        enemy->m_PetInfo.m_luck = 5;            //幸运
        enemy->m_PetInfo.m_power = 5;            //灵力

        //五行
        int j = 0;
        for(j = 0; j < 5; j++)
        {
            enemy->m_PetInfo.m_magic[j] = 5;
            enemy->m_magicHarm[j] = 0;
        }

        m_listMyPets.append(enemy);
    }
    return enemyCount;
}


GameModel::_GameStatus GameFightLogical::CheckGameOver()
{
    bool bMyWin = false, bEnemyWin = false;
    QList<_Pet*>::const_iterator ii;
    for(ii = m_listMyPets.constBegin(); ii < m_listMyPets.constEnd(); ii++)
    {
        switch ((*ii)->m_eJiaoSe)
        {
        case _Pet::JiaoSe_Self:
        case _Pet::JiaoSe_Friend:
            if((*ii)->remainHP() > 0)
                bMyWin = true;
            break;
        case _Pet::JiaoSe_Enemy:
            if((*ii)->remainHP() > 0)
                bEnemyWin = true;
            break;
        default:
            qCritical() << _T("[!GameLogical]CheckGameOver ERROR!");
            return setGameStatus(Game_Status_ERROR, -1);
            break;
        }
        //qDebug() << "(*ii)->remainHP:" << (*ii)->remainHP();
    }

    if(bMyWin == true && bEnemyWin == false)
    {
        m_eWinner = _Pet::JiaoSe_Self;
        return setGameStatus(Game_Status_GameOver, 0);
    }
    else if(bMyWin == false && bEnemyWin == true)
    {
        m_eWinner = _Pet::JiaoSe_Enemy;
        return setGameStatus(Game_Status_GameOver, 0);
    }

    return gameStatus();
}



GameModel::_GameStatus GameFightLogical::HuiHe(_UserChoice &userChoice)
{
    _Harm harm;
    QString str;
    _GameStatus retStatus;

    m_listMyPets[0]->m_attackProp = userChoice.choiceAttack;
    m_listMyPets[0]->m_defentProp = userChoice.choiceDefence;

    m_listMyPets[1]->m_defentProp = m_listMyPets[1]->m_attackProp = (int)Random(0, 5);
    //m_listMyPets[1]->defentProp = Math.floor(Math.random() * 5)

    emit s_fightMessage(QString("你使用%1攻击").arg(m_listMyPets[0]->m_attackProp));
    emit s_fightMessage(QString("你使用%1防御").arg(m_listMyPets[0]->m_defentProp));
    emit s_fightMessage(QString("敌人使用%1攻击").arg(m_listMyPets[1]->m_attackProp));
    emit s_fightMessage(QString("敌人使用%1防御").arg(m_listMyPets[1]->m_defentProp));


    harm = DoHarm(m_listMyPets[0],m_listMyPets[1]);
    m_listMyPets[1]->m_PetInfo.m_remainHP -= harm.nHarm;

    str = "属性使用";
    if(harm.nSuccessCode == 0)str += "Miss";
    else if(harm.nSuccessCode == 2)str += "成功";
    else if(harm.nSuccessCode == 3)str += "失败";
    else str += "无效果";
    str += QString(",你把敌人揍了%1血,还剩%2").arg(harm.nHarm).arg(m_listMyPets[1]->remainHP());
    emit s_fightMessage(str);

    retStatus = CheckGameOver();
    if(retStatus != Game_Status_Normal)
        return retStatus;
    /*
    if(m_listMyPets[1]->m_PetInfo.m_remainHP <= 0) {
        m_listMyPets[0]->m_PetInfo.m_remainHP = m_listMyPets[0]->m_PetInfo.m_maxHP;
        m_listMyPets[1]->m_PetInfo.m_remainHP = m_listMyPets[1]->m_PetInfo.m_maxHP;
        msgOutput.appendText("战斗胜利");
    }*/



    harm = DoHarm(m_listMyPets[1],m_listMyPets[0]);
    m_listMyPets[0]->m_PetInfo.m_remainHP -= harm.nHarm;

    str = "属性使用";
    if(harm.nSuccessCode == 0)str += "Miss";
    else if(harm.nSuccessCode == 2)str += "成功";
    else if(harm.nSuccessCode == 3)str += "失败";
    else str += "无效果";
    str += QString(",敌人把你揍了%1血,还剩%2").arg(harm.nHarm).arg(m_listMyPets[0]->remainHP());
    emit s_fightMessage(str);

    /*
    if(m_listMyPets[0]->m_PetInfo.m_remainHP <= 0) {
        m_listMyPets[0]->m_PetInfo.m_remainHP = m_listMyPets[0]->m_PetInfo.m_maxHP;
        m_listMyPets[1]->m_PetInfo.m_remainHP = m_listMyPets[1]->m_PetInfo.m_maxHP;
        msgOutput.appendText("战斗失败");
    }
    */

    emit s_fightMessage("--------------------------");

    qDebug() << "remainHP" << m_listMyPets[0]->remainHP() << m_listMyPets[1]->remainHP();

    return CheckGameOver();
}

//产生伤害算法
//HarmValueNum:攻击者,attack:被攻击
//sign:是否有附加伤害(属性相克),0表示无,1表示正,2表示负
//返回0表示miss,其他 伤害值
//PropValue表示属性值
GameFightLogical::_Harm GameFightLogical::DoHarm(_Pet *Pet1,_Pet *Pet2)
{
    _Harm harm;
    long t;
    //srand(Getms());
    if(RandTarget(Pet2->m_PetInfo.m_luck / 5 + Pet2->m_PetInfo.m_speed / 5))	//miss各占%20
    {
        harm.nSuccessCode = 0;
        return harm;
    }

    //计算攻击
    harm.nHarm = t = Pet1->m_PetInfo.m_attack;


    int attackPropValue = Pet1->m_PetInfo.m_magic[Pet1->m_attackProp];

    if((Pet1->m_attackProp + 1) % 5 == Pet2->m_defentProp)//属性攻击成功
    {
        harm.nSuccessCode = 2;
    }
    else if((Pet2->m_attackProp + 1) % 5 == Pet1->m_defentProp)//失败
    {
        harm.nSuccessCode = 3;
    }
    else
        harm.nSuccessCode = 1;

    if(harm.nSuccessCode == 2)		//附加使用成功
    {
        if(Pet1->m_attackProp == 2)		//雷属性
        {
            harm.nHarm = t * Random(attackPropValue + 1,attackPropValue * 4 + 1) / 100 + t;		//max <5倍
        }
        else			//其他属性
        {
            harm.nHarm = t * Random(attackPropValue + 1,attackPropValue * 2 + 1) / 100 + t;		//属性效果 <3倍
        }
    }
    else if(harm.nSuccessCode == 3)	//失败
    {
        harm.nHarm = t - t * Random((100 - attackPropValue) * 2,(100 - attackPropValue) * 5) / 500;		//属性效果 减小
    }
    else
        if(Pet1->m_attackProp == 2)	//雷,且无作用
            harm.nHarm = t * Random(attackPropValue/2,attackPropValue+1) /100 + t;		//max <5倍


    //计算防御
    //中 防御降低
    t = Pet2->m_PetInfo.m_defense;
    if(Pet2->m_magicHarm[4] != 0)	//对方中火
    {
        t = t * Random(2,50)/100;
    }

    harm.nHarm = harm.nHarm - t;		//攻击-防御
    t =	Pet1->m_PetInfo.m_attack;t=t * Random(Pet1->m_PetInfo.m_power,Pet1->m_PetInfo.m_power*2) /10000;	//灵力1~2倍	//攻击+灵力效果
    harm.nHarm = harm.nHarm	+ t;
    t = Pet1->m_PetInfo.m_attack;t=t * Random(10,Pet1->m_PetInfo.m_luck/10+1) /10000 ;	//吉运效果 千分之一到十分之一
    harm.nHarm = harm.nHarm	+ t;
    t =	Pet2->m_PetInfo.m_defense;t=t * Random(Pet2->m_PetInfo.m_power / 2,Pet2->m_PetInfo.m_power) /10000;	//防御+灵力效果
    harm.nHarm = harm.nHarm - t;
    t = Pet2->m_PetInfo.m_defense;t=t * Random(10,Pet2->m_PetInfo.m_luck / 10+1) /10000;	//吉运效果
    harm.nHarm = harm.nHarm - t;

        /*
        FightPets[1].Info[0];					//血
        FightPets[1].Info[2];				//攻击
        FightPets[1].Info[3];			//防御
        FightPets[1].Info[4];				//速度
        FightPets[1].Info[5];				//幸运
        FightPets[1].Info[6];				//灵力
        */
    if(harm.nHarm <= 0)harm.nHarm = Random(1,20);


    //QEvent event;
    //event.
    //QCoreApplication::sendEvent
    return harm;
}

bool GameFightLogical::isWin()
{
    return true;
}







/*
this->setText(QString("<center><h1>Move: (%1, %2)</h1></center>")
                                    .arg(QString::number(event->x()), QString::number(event->y())));
msg.sprintf("<center><h1>Release: (%d, %d)</h1></center>",
                                event->x(), event->y());
       this->setText(msg)

void NewPet()
{
    struct PetInfo &pet;
    &pet=&Pets[0];
    pet.picnum=2;		//图片号
    pet.busy=0;		//图片号
    pet.Info[0]=200;		//血
    pet.Info[1]=200;		//图片号
    pet.Info[2]=50;		//攻击
    pet.Info[3]=50;		//防御
    pet.Info[4]=50;		//速度
    pet.Info[5]=50;		//幸运
    pet.Info[6]=50;		//灵力
    pet.magic[0]=5;
    pet.magic[1]=5;
    pet.magic[2]=5;
    pet.magic[3]=5;
    pet.magic[4]=5;
    strcpy(pet.name,"小鹰");
    Potential=200;
    PetsNum++;

}


//重新排列宠物(去掉picnum为0的)
void SortPets()
{
    char i;
    char j;
    for(i=0;i<PetsNum;i++)
    {
        if(Pets[i].picnum==0)
        {
            if(MainPet>i)MainPet--;	//修复mainpet
            else if(MainPet==i && MainPet==PetsNum)MainPet--;	//MainPet为最后一个

            if(illpet>i && illpet<MaxPets)illpet--;	//修复illpet
            else if(illpet==i)illpet=255;

            for(j=i;j<PetsNum-1;j++)
            {
                memcpy(&Pets[j],&Pets[j+1],sizeof(struct PetInfo));
            }
            PetsNum--;
            Pets[PetsNum].picnum=0;
            continue;
        }
    }
}










 *
//产生(敌人属性)相对百分比的算法,CreateEnemys调用
//Rand为万分之多少为减去(不厉害)
//choice为2则两人,敌人会厉害点
int CreateEnemyDiffer(int Rand,char choice)
{
    char t;
    int n;
    int sign;		//+/-



    sign=1;
    if(choice==2)Rand=Rand/3;		//两人时 敌人多为加属性
    if(RandTarget(Rand))sign=-sign;	//rand%概率为减去
    t=Random(0,4);		//4中方案

    if(choice!=2) //单人
    {
        if(t==0)
        {
            n=Random(10,30);
            return n*sign;
        }
        else if(t==1)
        {
            n=Random(0,50);
            return n*sign;
        }
        else //2,3,4
        //0~100	//相当于一个正太分布函数
        {
            n=Random(0,1000);
            return Random(n/20,100-n/20)*sign;
        }
    }

    else	//双人
    {
        if(t==0)
        {
            n=Random(20,50);
            return n*sign;
        }
        else if(t==1)
        {
            n=Random(30,70);
            return n*sign;
        }
        else
        //0~100	//相当于一个正太分布函数
        {
            n=Random(400,1000);
            return Random(n/20,100-n/20)*sign;
        }
    }
}

//由Fight调用
//制作敌人,0为随机敌人,1为选择自己人为敌人(注意不能选同样的宠物,置标志),2直接返回
//返回0表示不再继续
//EnemyNum为0表示只创建头像,为1~3表示创建具体敌人
//choice:联机玩,只在创建敌人难度时起作用
char CreateEnemys(char FightType,char EnemyNum,char choice)
{
    char i;
    char j;
    char t1,t2;	//属性临时下标
    int t;		//属性返回临时
    struct PetInfo &pet;		//临时
    struct PetInfo &pet1;		//创建敌人的模板宠物

    int tGaiLv;		//临时概率,制作敌人时加减属性的概率(值越小敌人越可能厉害)

    if(FightType==0)
    {
        if(EnemyNum==0)		//只创建头像
        {
            EnemyNum=Random(1,4);		//1~3个敌人

            for(i=3;i<EnemyNum+3;i++)		//随机抽取宠物,提取相关值计算后为敌人值
            {
                &pet=&FightPets[i];
                pet.busy=0;
                pet.picnum=Random(1,MaxPetPic+1);
                strcpy(pet.name,"无名宠物");
            }
            for(;i<6;i++)		//剩下的数组
            {
                &pet=&FightPets[i];
                pet.picnum=0;
                pet.Info[1]=0;
            }
        }
        else
        {

            if(EnemyNum==1)tGaiLv=1000;
            else if(EnemyNum==2)tGaiLv=3000;
            else if(EnemyNum==3)tGaiLv=6000;

            for(i=3;i<EnemyNum+3;i++)		//随机抽取宠物,提取相关值计算后为敌人值
            {
                &pet=&FightPets[i];

                if(RandTarget(2000))	//一般概率从宠物队列中选取
                    &pet1=&Pets[Random(0,PetsNum)];
                else				//从FightPets中选取
                {
                    while(1)
                    {
                        j=Random(0,6);
                        if(j>2)j=j+3;
                        &pet1=&FightPets[j];
                        if(pet1.picnum==0)continue;
                        break;
                    }
                }
                //基本属性

                //血
                pet.Info[1] = pet1.Info[0] * (100+CreateEnemyDiffer(tGaiLv,choice)) / 90;
                //修正
                if(pet.Info[1]<=20)
                    pet.Info[1]=Random(50,100);
                else if(pet.Info[1]>MAXHP)
                    pet.Info[1]=MAXHP;
                pet.Info[0]=pet.Info[1];	//hp

                //属性
                t2=2;
                for(;t2<7;t2++)
                {
                        pet.Info[t2] = pet1.Info[t2] * (100+CreateEnemyDiffer(tGaiLv+500,choice)) / 110;

                    //修正
                    if(pet.Info[t2]<=10)
                        pet.Info[t2]=Random(20,40);
                    else if(pet.Info[t2]>MAXPROP3 && t2<4)
                        pet.Info[t2]=MAXPROP3;
                    else if(pet.Info[t2]>MAXPROP1 && t2>3)
                        pet.Info[t2]=MAXPROP1;
                }

                //随机系属性值
                t2=0;		//增加的属性个数
                while(1)
                {
                    if(RandTarget(8000)!=0)
                    {
                        if(t2==5)break;
                        t1=Random(0,5);
                        if(pet.magic[t1]!=0)continue;


                        t=pet1.magic[t1]*(100+CreateEnemyDiffer(tGaiLv,choice))/100;

                        if(t<=0)t=Random(1,10);
                        else if(t>=MAXPROP2)t=MAXPROP2;
                        pet.magic[t1]=t;	//

                        t2++;
                    }
                    else if(t2!=0)break;
                }//t2完
            }
        }
    }

    else if(FightType==1)
    {
        i=3;
        j=0;
        while(i<6)//选择战士,并将其标志为已用
        {
            &pet=&FightPets[i];
            j=ChoicePet(0,"选择练习宠物",j);		//临时用j
            if(j>=254)
            {
                if(i==3)return 0;
                else break;
            }
            memcpy(pet,&Pets[j],sizeof(struct PetInfo));
            Pets[j].busy=1;
            i++;
            if(CheckPetsBusy(0)<=1)break;	//起码剩余1个宠物去战斗
        }
        EnemyNum=i-3;
        for(;i<6;i++)			//填充其他
        {
            &pet=&FightPets[i];
            pet.picnum=0;
            pet.Info[1]=0;
        }
    }
    return EnemyNum;
}

 */
